#include "fsxwssettings.h"
using namespace FsxWsGUI;

//
// Constructor
//
FsxWsSettings::FsxWsSettings(QObject *parent) : QObject(parent),
    _fsxAircraftRadiusMeter(2000), _fsxUpdatePeriod(SIMCONNECT_PERIOD_SECOND),
    _fsxModelMaxWaypoints(2000), _fsxIpPort(500), _fsxIpAddress("127.0.0.1"),
    _fsxDumpStatistics(false),
    _guiUpdateAircraftsMs(5*1000), _guiShowWaypointsWhenAircraftSelected(true),
    _guiShowOnlyOwnAircraft(false),
    _kmlLineStyleWidth(1), _kmlLineStyleColor("ff000000"), _kmlPolyStyleColor("800000ff"),
    _kmlWaypointModel("PlaneTriangle.dae"),
    _kmlWhenSavedUseLocalModel(true), _kmlWhenSavedUseFullPath(true),
    _kmlNetworkLinkUpdatePeriodS(10), _dirFmcData(""),
    _autoConnectFsx(false), _autoStartWebServer(false)
{
    // code goes here
}

//
// Full URL of the model
//
QString FsxWsSettings::getKmlWaypointModelUrl() const
{
    QString url = FsxWsSettings::getKmlWaypointModelBaseUrl();
    if (!url.endsWith('/')) url.append("/");
    url.append(FsxWsSettings::getKmlWaypointModel());
    return url;
}

//
// Path, or URL of the model (depending on settings)
//
QString FsxWsSettings::getKmlWaypointModelPathOrUrl() const
{
    QString modelUrl;
    if (FsxWsSettings::getKmlWhenSavedUseLocalModel()) {
        if (FsxWsSettings::getKmlWhenSavedUseFullPath())
            modelUrl = QApplication::applicationDirPath () + "/docroot/kml/" + FsxWsSettings::getKmlWaypointModel();
        else
            modelUrl = "./" + FsxWsSettings::getKmlWaypointModel();
    } else {
        modelUrl = FsxWsSettings::getKmlWaypointModelUrl();
    }
    return modelUrl;
}

//
// Read the ini file
//
bool FsxWsSettings::read(const QString &file)
{
    QString f = (file.isEmpty()) ? QApplication::applicationDirPath() + "/config/FsxWs.ini" : file;
    if (!QFile::exists(f)) return false;

    FsxWsSettings::_settingsFilename = f;
    bool success = false;

    QSettings settings(f,QSettings::IniFormat, this);

    settings.beginGroup("fsx");
    int vi = settings.value("fsxAircraftRadiusMeter").toInt(&success);
    if (success) FsxWsSettings::setFsxAircraftRadiusMeter(vi);
    vi = settings.value("fsxModelMaxWaypoints").toInt(&success);
    if (success) FsxWsSettings::setFsxModelMaxWaypoints(vi);
    FsxWsSettings::setFsxModelMaxWaypointsDisabled(settings.value("fsxModelMaxWaypointsDisabled", QVariant(false)).toBool());
    vi = settings.value("fsxModelWaypointsTimeframeS").toInt(&success);
    FsxWsSettings::setFsxModelWaypointsTimeframeDisabled(settings.value("fsxModelWaypointsTimeframeDisabled", QVariant(false)).toBool());
    if (success) FsxWsSettings::setFsxModelWaypointsTimeframeS(vi);
    QString vs = settings.value("fsxIpAddress").toString();
    if (!vs.isEmpty()) FsxWsSettings::setFsxIpAddress(vs);
    vi = settings.value("fsxIpPort").toInt(&success);
    if (success) FsxWsSettings::setFsxIpPort(vi);
    settings.endGroup();

    settings.beginGroup("gui");
    FsxWsSettings::setGuiShowWaypointsWhenAircraftSelected(settings.value("guiShowWaypointsWhenAircraftSelected").toBool());
    FsxWsSettings::setGuiShowOnlyOwnAircraft(settings.value("guiShowOnlyOwnAircraft", QVariant(false)).toBool());
    vi = settings.value("guiUpdateAircraftsMs").toInt(&success);
    if (success) FsxWsSettings::setGuiUpdateAircraftsMs(vi);
    settings.endGroup();

    settings.beginGroup("kml");
    vs = settings.value("kmlLineStyleColor").toString();
    if (!vs.isEmpty()) FsxWsSettings::setKmlLineStyleColor(vs);
    vs = settings.value("kmlPolyStyleColor").toString();
    if (!vs.isEmpty()) FsxWsSettings::setKmlPolyStyleColor(vs);
    vi = settings.value("kmlLineStyleWidth").toInt(&success);
    if (success) FsxWsSettings::setKmlLineStyleWidth(vi);
    vs = settings.value("kmlWaypointModel").toString();
    if (!vs.isEmpty()) FsxWsSettings::setKmlWaypointModel(vs);
    vi = settings.value("kmlNetworkLinkUpdatePeriodS").toInt(&success);
    if (success) FsxWsSettings::setKmlNetworkLinkUpdatePeriodS(vi);
    FsxWsSettings::setKmlWhenSavedUseLocalModel(settings.value("kmlWhenSavedUseLocalModel").toBool());
    FsxWsSettings::setKmlWhenSavedUseFullPath(settings.value("kmlWhenSavedUseFullPath").toBool());
    settings.endGroup();

    settings.beginGroup("misc");
    FsxWsSettings::setAutoConnectFsx(settings.value("autoConnectFsx", QVariant(false)).toBool());
    FsxWsSettings::setAutoStartWebServer(settings.value("autoStartWebServer", QVariant(false)).toBool());
    vs = settings.value("navigraphDirectory").toString();
    if (!vs.isEmpty()) FsxWsSettings::setFmcDataDirectory(vs);
    settings.endGroup();

    return true;
}

//
// Write the ini file
//
bool FsxWsSettings::write() {
    if (FsxWsSettings::_settingsFilename.isEmpty()) return false;
    QSettings settings(FsxWsSettings::_settingsFilename,QSettings::IniFormat, this);
    if (!settings.isWritable()) return false;
    settings.beginGroup("fsx");
    settings.setValue("fsxAircraftRadiusMeter", FsxWsSettings::getFsxAircraftRadiusMeter());
    settings.setValue("fsxModelMaxWaypoints", FsxWsSettings::getFsxModelMaxWaypoints());
    settings.setValue("fsxModelWaypointsTimeframeS", FsxWsSettings::getFsxModelWaypointsTimeframeS());
    settings.setValue("fsxModelMaxWaypointsDisabled", FsxWsSettings::isFsxModelMaxWaypointsDisabled());
    settings.setValue("fsxModelWaypointsTimeframeDisabled", FsxWsSettings::isFsxModelWaypointsTimeframeDisabled());
    settings.setValue("fsxIpPort", FsxWsSettings::getFsxIpPort());
    settings.setValue("fsxIpAddress", FsxWsSettings::getFsxIpAddress());
    settings.endGroup();

    settings.beginGroup("gui");
    settings.setValue("guiShowWaypointsWhenAircraftSelected", FsxWsSettings::guiShowWaypointsWhenAircraftSelected());
    settings.setValue("guiUpdateAircraftsMs", FsxWsSettings::getGuiUpdateAircraftsMs());
    settings.setValue("guiShowOnlyOwnAircraft", FsxWsSettings::getGuiShowOnlyOwnAircraft());
    settings.endGroup();

    settings.beginGroup("kml");
    settings.setValue("kmlLineStyleWidth", FsxWsSettings::getKmlLineStyleWidth());
    settings.setValue("kmlLineStyleColor", FsxWsSettings::getKmlLineStyleColor());
    settings.setValue("kmlPolyStyleColor", FsxWsSettings::getKmlPolyStyleColor());
    settings.setValue("kmlWaypointModel", FsxWsSettings::getKmlWaypointModel());
    settings.setValue("kmlWhenSavedUseLocalModel", FsxWsSettings::getKmlWhenSavedUseLocalModel());
    settings.setValue("kmlWhenSavedUseFullPath", FsxWsSettings::getKmlWhenSavedUseFullPath());
    settings.setValue("kmlNetworkLinkUpdatePeriodS", FsxWsSettings::getKmlNetworkLinkUpdatePeriodS());
    settings.endGroup();

    settings.beginGroup("misc");
    settings.setValue("autoConnectFsx", FsxWsSettings::isAutoConnectFsx());
    settings.setValue("autoStartWebServer", FsxWsSettings::isAutoStartWebServer());
    settings.setValue("navigraphDirectory", FsxWsSettings::getFmcDataDirectory());
    settings.endGroup();

    settings.sync();
    return true;
}

//
// Get part of the settings for the FSX
//
FSXSimConnectSettings FsxWsSettings::toFsxSimConnectSettings()
{
    return FSXSimConnectSettings(
                FsxWsSettings::getFsxAircraftRadiusMeter(),
                FsxWsSettings::getFsxUpdatePeriod(),
                FsxWsSettings::isFsxDumpStatisticsEnabled(),
                FsxWsSettings::isFsxModelMaxWaypointsDisabled() ? -1 : FsxWsSettings::getFsxModelMaxWaypoints(),
                FsxWsSettings::isFsxModelWaypointsTimeframeDisabled() ? -1 : FsxWsSettings::getFsxModelWaypointsTimeframeS() * 1000,
                FsxWsSettings::getKmlWaypointModelUrl(),
                FsxWsSettings::getKmlNetworkLinkUpdatePeriodS()
                );
}

//
// Get part of the settings for KML
//
KmlSettings FsxWsSettings::toKmlSettings() {
    return KmlSettings(
                FsxWsSettings::getKmlLineStyleColor(),
                FsxWsSettings::getKmlLineStyleWidth(),
                FsxWsSettings::getKmlPolyStyleColor(),
                FsxWsSettings::getKmlWaypointModel(),
                FsxWsSettings::getKmlWaypointModelBaseUrl(),
                FsxWsSettings::getKmlNetworkLinkUpdatePeriodS()
                );
}
